package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Groups;
import com.ruoyi.system.service.IGroupsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分组Controller
 * 
 * @author ruoyi
 * @date 2024-02-09
 */
@RestController
@RequestMapping("/system/groups")
public class GroupsController extends BaseController
{
    @Autowired
    private IGroupsService groupsService;

    /**
     * 查询分组列表
     */
//    @PreAuthorize("@ss.hasPermi('system:groups:list')")
    @GetMapping("/selectGroupsByUserId")
    public R<?> selectGroupsByUserId()
    {
        return R.ok(groupsService.selectGroupsByUserId());
    }

    /**
     * 查询分组列表
     */
//    @PreAuthorize("@ss.hasPermi('system:groups:list')")
    @GetMapping("/list")
    public TableDataInfo list(Groups groups)
    {
        startPage();
        List<Groups> list = groupsService.selectGroupsList(groups);
        return getDataTable(list);
    }

    /**
     * 导出分组列表
     */
    @PreAuthorize("@ss.hasPermi('system:groups:export')")
    @Log(title = "分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Groups groups)
    {
        List<Groups> list = groupsService.selectGroupsList(groups);
        ExcelUtil<Groups> util = new ExcelUtil<Groups>(Groups.class);
        util.exportExcel(response, list, "分组数据");
    }

    /**
     * 获取分组详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:groups:query')")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId)
    {
        return success(groupsService.selectGroupsByGroupId(groupId));
    }

    /**
     * 新增分组
     */
    @PreAuthorize("@ss.hasPermi('system:groups:add')")
    @Log(title = "分组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Groups groups)
    {
        return toAjax(groupsService.insertGroups(groups));
    }

    /**
     * 修改分组
     */
    @PreAuthorize("@ss.hasPermi('system:groups:edit')")
    @Log(title = "分组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Groups groups)
    {
        return toAjax(groupsService.updateGroups(groups));
    }

    /**
     * 删除分组
     */
    @PreAuthorize("@ss.hasPermi('system:groups:remove')")
    @Log(title = "分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds)
    {
        return toAjax(groupsService.deleteGroupsByGroupIds(groupIds));
    }

    /**
     * 查询分组列表
     */
    @Log(title = "分组", businessType = BusinessType.OTHER)
	@PostMapping("/selectFriendGroups")
    public R<?> remove()
    {
        return R.ok(groupsService.selectFriendGroups());
    }
}
